﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Silverlight.Testing;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System8ToyMC;
using System8Equations;
using System.Windows.Automation.Peers;
using System.Windows.Automation.Provider;

namespace t_System8ToyMCControls
{
    [TestClass]
    public class t_RandomVariableList : PresentationTest
    {
        RandomVariableList _val = null;
        ListBox _lb = null;
        Button _clear = null;

        [TestInitialize]
        public void TestSetup()
        {
            _val = new RandomVariableList();
            _lb = _val.FindName("VList") as ListBox;
            _clear = _val.FindName("ClearButton") as Button;
            TestPanel.Children.Add(_val);
        }

        [TestMethod]
        public void TestCTor()
        {
            Assert.IsNotNull(_val);
            Assert.IsNotNull(_lb);
            Assert.IsNotNull(_clear);
        }

        [TestMethod]
        [Asynchronous]
        [Timeout(1000)]
        public void TestAddSingleItem()
        {
            int count = 0;
            _val.VariableAdded += v => count++;
            UniformS8Variable uv = new UniformS8Variable(5.0) { Path = "n/TotalValue", MinValue = 0.0, MaxValue = 10.0 };
            EnqueueCallback(() => _val.AddVariable(uv));
            EnqueueConditional(() => _lb.Items.Count > 0);
            EnqueueCallback(() => Assert.AreEqual(1, _lb.Items.Count, "# of items isn't correct"));
            EnqueueCallback(() => Assert.AreEqual(1, count, "Variable list updated event fired incorrect number of times"));
            EnqueueTestComplete();
        }

        [TestMethod]
        [Asynchronous]
        [Timeout(1000)]
        public void TestClearItem()
        {
            ButtonAutomationPeer buttonPeer = new ButtonAutomationPeer(_clear);
            IInvokeProvider buttonInvoker = (IInvokeProvider)buttonPeer;

            int count = 0;
            _val.VariableAdded += v => count++;
            int isCleared = 0;
            _val.VariablesCleared += () => isCleared++;

            UniformS8Variable uv = new UniformS8Variable(5.0) { Path = "n/TotalValue", MinValue = 0.0, MaxValue = 10.0 };
            EnqueueCallback(() => _val.AddVariable(uv));
            EnqueueConditional(() => _lb.Items.Count > 0);

            EnqueueCallback(() => buttonInvoker.Invoke());
            EnqueueConditional(() => isCleared != 0);
            EnqueueCallback(() => Assert.AreEqual(1, isCleared, "isCleared called the wrong number of times"));
            EnqueueCallback(() => Assert.AreEqual(0,_lb.Items.Count, "List doesn't seem to be empty!"));
            EnqueueTestComplete();
        }

        [TestMethod]
        [Asynchronous]
        [Timeout(1000)]
        public void TestAddCallback()
        {
            S8Variable calledVar = null;
            _val.VariableAdded += v => calledVar = v;
            UniformS8Variable uv = new UniformS8Variable(5.0) { Path = "n/TotalValue", MinValue = 0.0, MaxValue = 10.0 };
            EnqueueCallback(() => _val.AddVariable(uv));
            EnqueueConditional(() => _lb.Items.Count > 0);
            EnqueueCallback(() => Assert.AreEqual("n/TotalValue", calledVar.Path, "The wrong path came back!"));
            EnqueueTestComplete();
        }

        [TestMethod]
        [Asynchronous]
        [Timeout(1000)]
        public void TestAddTwoItems()
        {
            UniformS8Variable uv1 = new UniformS8Variable(5.0) { Path = "n/TotalValue", MinValue = 0.0, MaxValue = 10.0 };
            UniformS8Variable uv2 = new UniformS8Variable(5.0) { Path = "p/TotalValue", MinValue = 0.0, MaxValue = 10.0 };
            EnqueueCallback(() => _val.AddVariable(uv1));
            EnqueueCallback(() => _val.AddVariable(uv2));
            EnqueueConditional(() => _lb.Items.Count > 0);
            EnqueueCallback(() => Assert.AreEqual(2, _lb.Items.Count, "Expected 2 items on the list"));
            EnqueueTestComplete();
        }

        [TestMethod]
        [Asynchronous]
        [Timeout(1000)]
        public void TestAddTwoSameItems()
        {
            int count = 0;
            _val.VariableAdded += v => count++;
            UniformS8Variable uv1 = new UniformS8Variable(5.0) { Path = "n/TotalValue", MinValue = 0.0, MaxValue = 10.0 };
            UniformS8Variable uv2 = new UniformS8Variable(5.0) { Path = "n/TotalValue", MinValue = 0.0, MaxValue = 210.0 };
            EnqueueCallback(() => _val.AddVariable(uv1));
            EnqueueCallback(() => _val.AddVariable(uv2));
            EnqueueConditional(() => _lb.Items.Count > 0);
            EnqueueCallback(() => Assert.AreEqual(1, _lb.Items.Count, "List should have only 1 item on it"));
            EnqueueCallback(() => Assert.AreEqual(210.0, (_lb.Items[0] as UniformS8Variable).MaxValue, "The second wasn't kept"));
            EnqueueCallback(() => Assert.AreEqual(2, count, "VariableListUpdated fired incorrect number of times"));
            EnqueueTestComplete();
        }

        [TestMethod]
        [Asynchronous]
        public void TestShowIt()
        {
            UniformS8Variable uv1 = new UniformS8Variable(5.0) { Path = "n/TotalValue", MinValue = 0.0, MaxValue = 10.0 };
            UniformS8Variable uv2 = new UniformS8Variable(5.0) { Path = "p/TotalValue", MinValue = 0.0, MaxValue = 10.0 };
            EnqueueCallback(() => _val.AddVariable(uv1));
            EnqueueCallback(() => _val.AddVariable(uv2));
            EnqueueConditional(() => _lb.Items.Count > 0);
            EnqueueDelay(1000);
            EnqueueTestComplete();
        }
    }
}